package chapter5.homework.heima.Q2ReceiveAndFeedback.teachers;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @Project ：learn_java
 * @File ：Client.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/14 09:07
 * @Description ：无论是客户端还是服务端，两者的读取和发送代码逻辑都是一样的，
 * 代码部分可以复用，不管是使用OutputStreamWriter、OutputStreamReader还是
 * 用BufferedReader、BufferedWriter再包装一次都是一样的
 * */

public class Client {
    public static void main(String[] args) throws IOException {

        String localhost = "127.0.0.1";
        int defaultPort = 10000;
        // 创建socket对象并连接服务端
        Socket socket = new Socket(localhost, defaultPort);

        String str = "很高兴见到你\nI suddenly remembered something from the past.";
        OutputStream os = socket.getOutputStream();
        os.write(str.getBytes());

        // 写一个结束标记
        socket.shutdownOutput();

        // same code as the Server, as the same logic
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int b;
        while ((b = isr.read()) != - 1) {
            System.out.print((char) b);
        }

        socket.close();
    }
}
